﻿#include "ChannelWidget.h"
#include "Log.h"
#include <qevent.h>
#include <qaction.h>
#include <qmenu.h>
#include "SocketSystem.h"
#include "Note.h"

ChannelWidget::ChannelWidget(const ChannelCreateInfo* i) :
	QFrame{ nullptr },
	current_btn{ Qt::NoButton },
	info{ *i },
	channel{ },
	ui_state{ CHANNEL_STATE_MAX }
{
	ui.setupUi(this);
	connect(ui.btn_close, &QToolButton::clicked, this, &ChannelWidget::slot_closed);
	start_server(i);
}

ChannelWidget::~ChannelWidget()
{
	Logger().info("destory");
	DestoryChannel(this->channel);
}

void ChannelWidget::mousePressEvent(QMouseEvent* event)
{
	this->current_btn = event->button();
	this->current_mouse_point = event->globalPos() - this->pos();
}

void ChannelWidget::mouseReleaseEvent(QMouseEvent* event)
{
	if(event->button() == this->current_btn)
	{
		this->current_btn = Qt::NoButton;
	}
}

void ChannelWidget::mouseMoveEvent(QMouseEvent* event)
{
	if(current_btn == Qt::LeftButton)
	{
		this->move(event->globalPos() - this->current_mouse_point);
	}
}

void ChannelWidget::mouseDoubleClickEvent(QMouseEvent* event)
{

}

void ChannelWidget::contextMenuEvent(QContextMenuEvent* event)
{
	QAction act_start_server(QIcon{ ":/menu/start" }, "启动服务");
	QAction act_stop_server(QIcon{ ":/menu/stop" }, "停止服务");
	QMenu menu_context{};
	menu_context.addAction(&act_start_server);
	menu_context.addAction(&act_stop_server);

	bool start_state_b = channel == nullptr;

	act_start_server.setEnabled(start_state_b);
	//act_stop_server.setEnabled(stop_b);

	auto res = menu_context.exec(event->globalPos());
	if(res == &act_start_server)
	{
		start_server(nullptr);
	}
	if(res == &act_stop_server)
	{
		stop_server();
	}

}

static std::array<std::pair<QString, QString>, CHANNEL_STATE_MAX>& ui_state_styles()
{
	static std::array<std::pair<QString, QString>, CHANNEL_STATE_MAX> sty{
		std::pair{"background-color:green;","已启动"},// CHANNEL_STATE_START,
		std::pair{"background-color:red;","未启动"},//	CHANNEL_STATE_UNSTART,
		std::pair{"background-color:yellow;","已连接"},//	CHANNEL_STATE_CONNECTED,
		std::pair{"background-color:blue;","正在链接"},//	CHANNEL_STATE_CONNECTING,
		std::pair{"background-color:pink;","未连接"},//	CHANNEL_STATE_UNCONNECT,
		std::pair{"background-color:lightblue;","操作超时"},//	CHANNEL_STATE_TIMEOUT,
	};
	return sty;
}

void ChannelWidget::slot_check_state(CHANNEL_STATE st)
{
	if(st != ui_state)
	{
		ui_state = st;
		auto& sty = ui_state_styles()[st];
		this->ui.lab_color_state->setStyleSheet(sty.first);
		this->ui.lab_color_state->setText(sty.second);
	}
}

//void ChannelWidget::slot_timer_check_state()
//{
//	if(channel != nullptr)
//	{
//		slot_check_state(channel->state);
//	}
//}

void ChannelWidget::start_server(const ChannelCreateInfo* i)
{
	if(channel != nullptr)
	{
		Note nt{ "当前通道已经启动，不需要再次启动" };
		nt.showDialog();
		return;
	}

	channel = ((i == nullptr) ? CreateChannel(&info) : CreateChannel(i));

	if(channel != nullptr)
	{
		ui.lab_proto_name->setText(channel->get_proto_name());
		ui.lab_title->setText(channel->get_info().title);
		slot_check_state(channel->state);
		ui.lab_port->setText(QString::number(channel->get_local_port()));

		connect(channel, &Channel::sig_state_changed, this, &ChannelWidget::slot_check_state);
	}
	else
	{
		ui.lab_proto_name->setText("");
		slot_check_state(CHANNEL_STATE_UNSTART);
		ui.lab_port->setText(QString::number(i->input_address.port));
	}
}

void ChannelWidget::stop_server()
{
	DestoryChannel(channel);
	channel = nullptr;
	slot_check_state(CHANNEL_STATE_UNSTART);
}



void ChannelWidget::slot_closed()
{
	emit sig_closed(this);
}

void ChannelWidget::connect_action()
{
	
}
